package com.ddwl.shop.service;

import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.exception.ParamException;
import com.ddwl.common.util.BeanConverUtils;
import com.ddwl.common.util.UUIDGenerator;
import com.ddwl.schema.bo.shop.AgentChannelBo;
import com.ddwl.schema.vo.shop.AgentChannelVo;
import com.ddwl.shop.dao.AgentChannelDao;
import com.ddwl.shop.dao.model.AgentChannel;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author hemiaojun
 * @Date 2021/7/22 10:21
 */
@Service
public class AgentChannelService {

    @Resource
    private AgentChannelDao agentChannelDao;


    @Transactional(rollbackFor = Exception.class)
    public String save(AgentChannelBo bo) {
        AgentChannel ac = BeanConverUtils.convertBean(bo, AgentChannel.class);
        if (null == ac.getStatus()) {
            ac.setStatus(BaseEnum.StatusEnum.ENABLE.getCode());
        }
        ac.setId(UUIDGenerator.getShortUUID());
        agentChannelDao.insert(ac);
        return ac.getId();
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(AgentChannelBo bo){
        ParamException.isNull(bo.getId());
        AgentChannel ac = BeanConverUtils.convertBean(bo, AgentChannel.class);
        if (null == ac.getStatus()) {
            ac.setStatus(BaseEnum.StatusEnum.ENABLE.getCode());
        }
        agentChannelDao.updateByPrimaryKeySelective(ac);
    }

    public AgentChannelVo get(String id) {
        return BeanConverUtils.convertBean(agentChannelDao.selectByPrimaryKey(id), AgentChannelVo.class);
    }

    public void delete(String id) {
        agentChannelDao.deleteById(id);
    }

    public List<AgentChannelVo> all() {
        AgentChannel entity = new AgentChannel();
        entity.setStatus(BaseEnum.StatusEnum.ENABLE.getCode());
        entity.setDflag(BaseEnum.DflagEnum.NOT_DELETE.getCode());
        return BeanConverUtils.convertBeanList(agentChannelDao.select(entity), AgentChannelVo.class);
    }
}
